package com.yungam.other.mes_controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.yungam.common.bean.work_order.WorkOrder;
import com.yungam.common.bean.work_order.WorkOrderHistory;
import com.yungam.common.bean.work_order.WorkOrderProduct;
import com.yungam.common.bean.work_order.WorkOrderProductHistory;
import com.yungam.common.service.WorkOrderHistoryService;
import com.yungam.common.service.WorkOrderProductHistoryService;
import com.yungam.common.service.WorkOrderProductService;
import com.yungam.common.service.WorkOrderService;import com.yungam.other.response.ResData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 工单开始
 */
@Slf4j
@RestController
public class StartWorkOrderNoController {

    @Resource
    private WorkOrderHistoryService workOrderHistoryService;
    @Resource
    private WorkOrderProductHistoryService workOrderProductHistoryService;
    @Resource
    private WorkOrderService workOrderService;
    @Resource
    private WorkOrderProductService workOrderProductService;


    @PostMapping("/IMCS/startWorkOrderNo")
    public Map<String, Object> startWorkOrderNo(@RequestBody Map<String, Object> params) {
        log.info("========= START『mes调用imcs』[工单开始]=========");
        log.info("=========『mes数据』{}", new Gson().toJson(params));

        String workOrderNo = String.valueOf(params.get("workOrderNo"));
        WorkOrderHistory woh =
                workOrderHistoryService.getOne(new QueryWrapper<WorkOrderHistory>()
                        .eq("work_order_no", workOrderNo));
        if (woh == null) {
            log.error("mes 传来的工单号不存在");
            return ResData.error("mes 传来的工单号不存在");
        }

        // 将 history 表信息复制到 工作表 先删后插入
        workOrderService.remove(new QueryWrapper<WorkOrder>().eq("work_order_no", workOrderNo));
        WorkOrder workOrder = new WorkOrder();
        workOrder.setWorkOrderNo(woh.getWorkOrderNo());
        workOrder.setProductNo(woh.getProductNo());
        workOrder.setProductName(woh.getProductName());
        workOrder.setQty(woh.getQty());
        workOrder.setImportTime(new Date());
        workOrderService.save(workOrder);

        List<WorkOrderProductHistory> workOrderProducts = workOrderProductHistoryService.list(new QueryWrapper<WorkOrderProductHistory>()
                .eq("work_order_no", workOrderNo));
        if (workOrderProducts.size() == 0) {
            log.info("该工单无罐号");
            return ResData.success(null);
        }

        // 先删除该工单的罐号, 再重新插入
        workOrderProductService.remove(new QueryWrapper<WorkOrderProduct>()
                .eq("work_order_no", workOrderNo));

        List<WorkOrderProduct> insert = Lists.newArrayList();
        for (WorkOrderProductHistory ph : workOrderProducts) {
            WorkOrderProduct workOrderProduct = new WorkOrderProduct();
            workOrderProduct.setWorkOrderNo(ph.getWorkOrderNo());
            workOrderProduct.setProductSn(ph.getProductSn());
            workOrderProduct.setProductNo(ph.getProductNo());
            workOrderProduct.setDate(new Date());
            workOrderProduct.setState(ph.getState());
            insert.add(workOrderProduct);
        }

        if (!insert.isEmpty()) {
            workOrderProductService.saveBatch(insert);
        }
        log.info("工单开始成功");
        return ResData.success("成功");
    }

}
